package 双指针;

import java.util.Scanner;

public class test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = in.nextInt();
        }
        test2 test2 = new test2();
        test2.duplicateZeros(nums); //实例化
    }
    public void duplicateZeros(int[] nums){
        // 从后往前遍历
        int index = -1;
        int cur = 0;
        int n = nums.length;
        for (; cur < n; cur++) {
            if(nums[cur]==0)
                index+=2; //此时需要占两个位
            else
                index++;
            if(index>=n-1)
                break; //已经到了最多能返回的个数
        }
        if(index==n){
            //当前位置不能写入到数组 而且只有复写的情况才会越界
            cur--;
            nums[n-1] = 0;
            index -= 2;
        }
        for (; cur >= 0; cur--) {
            if(nums[cur]==0){
                nums[index] = nums[index-1] = 0;
                index-=2;
            }else {
                nums[index] = nums[cur];
                index--;
            }
        }
    }
}
